package com.ignou.mca.exam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.beans.ExamBean;
import com.ignou.mca.beans.ExamStatusBean;
import com.ignou.mca.beans.QuestionsBean;
import com.ignou.mca.datasource.DatabaseOperations;

/**
 * Servlet implementation class EndExam
 */
public class EndExam extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EndExam() 
    {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String exam = request.getParameter("exam");
		String subject = request.getParameter("subjectId");
		String answersSelected = request.getParameter("selectedAnswers");
		String userName = request.getParameter("userName");
		String result="";
		
		int marks = 0;
		int totalMarks = 0;
		ServletContext ctx = getServletContext();
		HttpSession session = request.getSession();
		
		ArrayList<QuestionsBean> ques = (ArrayList<QuestionsBean>) session.getAttribute("ExamQuestions");
		
		
		if(ques.size()>0)
		{
			StringTokenizer st = new StringTokenizer(answersSelected,"|");
			int quest = 0;
			
			while(st.hasMoreTokens())
			{
				QuestionsBean qb = ques.get(quest);
				if(qb.getCorrectAnswer().equals(st.nextToken()))
				{
					marks =+ qb.getMarks();
				}
			}
			for(int i=0;i<ques.size();i++)
			{
				QuestionsBean qbb = ques.get(i);
				totalMarks =+ qbb.getMarks();
			}
		}
		
		int calPer = (marks*100)/totalMarks;
		
		if(calPer>=0 && calPer<35)
		{
			result = "failed";
		}
		else if(calPer>35 && calPer<50)
		{
			result = "passed";
		}else if(calPer>51 && calPer<60)
		{
			result = "good";
		}
		else if(calPer>61 && calPer<80)
		{
			result = "very good";
		}
		else if(calPer>81)
		{
			result = "excellent";
		}
		
		DatabaseOperations dbOps = new DatabaseOperations();
		ExamStatusBean esb = new ExamStatusBean();
		esb.setExamId(Integer.parseInt(exam));
		esb.setResult(result);
		esb.setScore(marks);
		esb.setSubjectId(Integer.parseInt(subject));
		esb.setUserName(userName);
		
		ExamStatusBean es = dbOps.updateExamStatus(esb);
		if(es!=null)
		{
			session.setAttribute("ExamStatus", es);
			response.sendRedirect("./ShowExamResult.jsp");
		}else
		{
			 session.setAttribute("ErrorMessage", "Unable to get Exam result. Please contact administrator.");
			 ctx.getRequestDispatcher("/ErrorDisplay.jsp").forward(request, response);
		}
	}

}
